package GameServer.core;

import GameServer.Demo;
import GameServer.manager.module1.Demo1Mgr;
import GameServer.manager.module6.Demo6Mgr;
import GameServer.net.NetFramework;
import GameServer.net.ProtoIds;
import GameServer.net.ProtoMessage;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * Created by gzhd on 2017/11/29.
 */
public class Router {
    private static Router router;
    public Demo1Mgr demo1Mgr;
    public Demo6Mgr demo6Mgr;


    private Router() {
        demo1Mgr = Demo1Mgr.getInstance();
        demo6Mgr = Demo6Mgr.getInstance();
    }

    public static Router getInstance() {
        if (router == null) {
            router = new Router();
        }
        return router;
    }

    public void initData() {
        Demo.print("Router initData");
        demo1Mgr.initData();
    }


    public void handle(NetFramework netFramework, JSONObject request) throws Exception {
        if (!request.containsKey("protoid")) {
            NetFramework.inst.write(JSON.parseObject(JSON.toJSONString(ProtoMessage.getErrorResp("没有协议号"))));
            return;
        }
        int protoId = request.getInteger("protoid");
        ProtoMessage reqmsg = JSON.parseObject(request.toJSONString(), ProtoMessage.class);
        if (!request.containsKey("userid")) {
            NetFramework.inst.write(JSON.parseObject(JSON.toJSONString(ProtoMessage.getErrorResp("没有userid"))));
            return;
        }

        long userid = reqmsg.getUserid();
        switch (protoId){

            case ProtoIds.TEST:
                JSONArray ret = new JSONArray();
                ret.add("测试接口");
                NetFramework.inst.write(ret);
                break;
            case ProtoIds.DEMO_LOGIC_1:
                demo1Mgr.demoLogic1(netFramework,reqmsg,userid);
                break;
            case ProtoIds.DEMO_LOGIC_6:
                demo6Mgr.demoLogic6(netFramework,reqmsg,userid);
                break;

        }


    }


}
